package top.igotcha.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.igotcha.eduservice.client.OrderClient;
import top.igotcha.eduservice.entity.EduCourse;
import top.igotcha.eduservice.entity.frontvo.CourseFrontVo;
import top.igotcha.eduservice.entity.frontvo.CourseWebVo;
import top.igotcha.eduservice.entity.vo.ChapterVo;
import top.igotcha.eduservice.service.EduChapterService;
import top.igotcha.eduservice.service.EduCourseService;
import top.igotcha.commonutils.ordervo.CourseWebVoOrder;
import top.igotcha.commonutils.utils.JwtUtils;
import top.igotcha.commonutils.utils.R;


import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static top.igotcha.commonutils.utils.JwtUtils.APP_SECRET;

@RestController
@RequestMapping("/eduservice/coursefront")
//@CrossOrigin
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    //1 条件查询带分页查询课程
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(@PathVariable long page, @PathVariable long limit,
                                @RequestBody(required = false) CourseFrontVo courseFrontVo) {
        Page<EduCourse> pageCourse = new Page<>(page,limit);
        Map<String,Object> map = courseService.getCourseFrontList(pageCourse,courseFrontVo);
        //返回分页所有数据
        return R.OK().data(map);
    }

    //2 课程详情的方法
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request) {
        //根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);

        //根据课程id查询章节和小节
        List<ChapterVo> chapterVideoList = chapterService.getChapterVideoByCourseId(courseId);

        //根据课程ID和用户ID查询当前课程是否已支付
        //System.out.println("=============");
        //System.out.println(request);
        //System.out.println(request.getParameter("token"));
        //System.out.println(request.getHeaders("token"));
        System.out.println( JwtUtils.getMemberIdByJwtToken(request));

        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(request.getParameter("token"));
        Claims claims = claimsJws.getBody();
        boolean isBuyCourse =orderClient.isBuyCourse(courseId, (String) claims.get("id"));

        //boolean isBuyCourse =orderClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.OK().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVideoList).data("isBuy",isBuyCourse);
    }

    @ApiOperation("根据课程ID查询课程信息，用于远程调用")
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id){
        CourseWebVo courseInfo = courseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo,courseWebVoOrder);
        return courseWebVoOrder;
    }
}












